package com.dosin.crowno;


import com.dosin.crowno.pojo.UserAccount;
import com.dosin.crowno.service.UserAccountService;
import com.sun.tools.internal.xjc.reader.xmlschema.bindinfo.BIConversion;
import javafx.collections.FXCollections;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyCodeCombination;
import javafx.scene.input.KeyCombination;
import javafx.scene.input.Mnemonic;
import javafx.stage.Stage;
import javafx.stage.Window;
import javafx.stage.WindowEvent;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;



@Component
public class LoginController  {

    @Resource
    private UserAccountService userAccountService;
    @Resource
    private MainController mainController;

    @FXML
    private Button cancelButton;
    @FXML
    private Button loginButton;
    @FXML
    private Label loginMassageLabel;

    @FXML
    private ImageView brandingImageView;
    @FXML
    private ImageView lockImageView;

    @FXML
    private TextField usernameTextField;
    @FXML
    private PasswordField passwordField;




    //登录
    public void loginButtonOnAction() throws IOException {

        loginMassageLabel.setText("");
        if(usernameTextField.getText().isEmpty() == false && passwordField.getText().isEmpty() == false){

            validateLogin();

        }else {

            loginMassageLabel.setText("请输入用户名和密码！");
        }

    }

    public void usernameTextFieldOnAction(){
        if(!loginMassageLabel.getText().isEmpty()) {
            loginMassageLabel.setText("");
        }

    }

    public void passwordFieldOnAction(){
            loginMassageLabel.setText("");
        if(!loginMassageLabel.getText().isEmpty()) {
            loginMassageLabel.setText("");
        }

    }





    //点击取消按钮退出程序
    public void cancelButtonAction(){
        Stage stage = (Stage) cancelButton.getScene().getWindow();
        stage.close();
        System.exit(0);
    }


    //登录效验
    private void validateLogin() throws IOException {


        loginMassageLabel.setText("");
        String username = usernameTextField.getText();
        String password = passwordField.getText();
        UserAccount user = userAccountService.userAndPass(username, password);
        Long datetime = userAccountService.now().getTime();

        //    @Select("select * from user_account WHERE username=#{username} AND password=#{password}")
        //    UserAccount  selectByUserAndPass(String username,String password);
        if(userAccountService.login(username,password)){
            if(user.getIsLogin()==0 && datetime <= user.getDueDate().getTime()) {

                mainController.openMainWindow(user,usernameTextField);

                user.setAccountId(user.getAccountId());
                System.out.println(user.getAccountId().toString());
                user.setIsLogin(0);
                userAccountService.updateByPrimaryKeySelective(user);

            }else if(user.getIsLogin()==0 && datetime > user.getDueDate().getTime()){

                loginMassageLabel.setText("您的软件授权已到期，请联系管理员！");

            }else{
                    loginMassageLabel.setText(" 此用户已登录！");

            }
        }else {
            loginMassageLabel.setText(" 用户名或密码错误，请重新输入！");
        }






    }





    }











